<?php
/**
* 随机红包的金额生产
* @param total float 总金额
* @param num float 数量
* @param min float 每个人最少能收到0.01元  
* @return float 随机钱
**/
function pocket($total, $num, $min = 0.01){
    for ($i=1; $i<$num; $i++) {
        $safe_total = ($total-($num-$i)*$min)/($num-$i);
        $money = mt_rand($min*100,$safe_total*100)/100;
        $total = $total-$money;
        $arr['res'][$i] = array(
            'i' => $i,
            'money' => $money,
            'total' => $total
        );
    }
    $arr['res'][$num] = array('i'=>$num,'money'=>$total,'total'=>0);
    return $arr['res'][rand(1,$num)]['money'];
}

/**
* 数组转中文日期
* @param array days [0,1,3,4,5,6,7]
**/
function weekdays(array $days){
    $weekarray = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    $week = [];
    foreach ($days as $value) {
        $week[] = $weekarray[$value];
    }
    return $week;
}

/**
 * 百度地图---->腾讯地图
 * @param double $lat 纬度
 * @param double $lng 经度
 * @return array();
 */
function bdMap_to_txMap($lat,$lng){
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng - 0.0065;
    $y = $lat - 0.006;
    $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta);
    $lat = $z * sin($theta);
    return array('lng'=>$lng,'lat'=>$lat);
}
 
/**
 * 腾讯地图---->百度地图
 * @param double $lat 纬度
 * @param double $lng 经度
 */
function txMap_to_bdMap($lat,$lng){
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng;
    $y = $lat;
    $z =sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta) + 0.0065;
    $lat = $z * sin($theta) + 0.006;
    return array('lng'=>$lng,'lat'=>$lat);
}

/**
 * 根据经纬度算距离，返回结果单位是公里，先纬度，后经度
 * @param $lat1
 * @param $lng1
 * @param $lat2
 * @param $lng2
 * @return float|int
 */
function getDistance($lng1,$lat1,$lng2,$lat2){
    $earthRadius = 6367000;
    $lat1 = deg2rad($lat1);
    $lng1 = deg2rad($lng1);
    $lat2 = deg2rad($lat2);
    $lng2 = deg2rad($lng2);
    $calcLongitude = $lng2 - $lng1; 
    $calcLatitude = $lat2 - $lat1; 
    $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); 
    $stepTwo = 2 * asin(min(1, sqrt($stepOne))); 
    $calculatedDistance = $earthRadius * $stepTwo; 
    $distance = round($calculatedDistance); 
    if($distance <= 500){
        return $distance.'m';
    }else{
        return round($distance/1000,1).'km';
    }
}

/**
 * @description:根据数据 
 * @param {dataArr:需要分组的数据；keyStr:分组依据} 
 * @return: 
 */
function dataGroup(array $dataArr,string $keyStr){
    $newArr = [];
    foreach ($dataArr as $val) { 
        $newArr[$val[$keyStr]][] = $val;
    }
    return $newArr;
}